﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetOrientDB.ObjectModel;
using NetOrientDB.HttpClient;
using Newtonsoft.Json.Linq;

namespace Sample
{
	class Program
	{
		// server uri
		static string server = "http://localhost:2480";
		// database name
		static string database = "netorientdb";
		// database user
		static string username = "admin";
		// database user password
		static string password = "admin";

		static void Main(string[] args)
		{
			// create database connection
			using (Connection cn = new Connection(server, database, username, password))
			{
				// connects to database
				cn.Connect();

				// prints server info
				Console.WriteLine("Server:");
				Console.WriteLine("\t{0}/{1}", server, database);
				Console.WriteLine("");

				// print classes defined at database
				Console.WriteLine("Classes:");
				foreach (var cls in cn.Classes)
				{
					Console.WriteLine("\tId:{0} Name:'{1}' Records:{2}", cls.Id, cls.Name, cls.Records);
					foreach (var p in cls.Properties)
					{
						Console.WriteLine("\t\tId:{0} Name:'{1}' Type:{2}", p.Id, p.Name, p.Type);
					}
				}

				// ensure classes
				EnsureClasses(cn);

				Console.WriteLine("");
				int maxDocs = 10000;
				Console.WriteLine("Creating {0} docs...", maxDocs);
				var startDate = DateTime.Now;				

				// create documents
				List<string> documentRids = new List<string>();
				var rnd = new Random(DateTime.Now.Millisecond);
				for (var i = 0; i < maxDocs; i++)
				{
					var doc = new OrientDocument { ClassName = "user" };
					doc.SetProperty<string>("username", string.Format("user{0}", i));
					doc.SetProperty<byte>("age", (byte)rnd.Next(0, 100));

					var rid = cn.InsertDocument(doc);
					documentRids.Add(rid);
					doc = null;
				}
				Console.WriteLine("{0} docs created, took {1} ms...\r\n", maxDocs, DateTime.Now.Subtract(startDate).TotalMilliseconds);

				// update the documents age property
				Console.WriteLine("Fetch & Update {0} docs...", maxDocs);
				startDate = DateTime.Now;
				foreach (var rid in documentRids)
				{
					var doc = cn.GetDocumentByRid(rid);
					doc.SetProperty<byte>("age", (byte)rnd.Next(0, 100));
					cn.UpdateDocument(doc);
					doc = null;
				}
				Console.WriteLine("{0} docs fetched and updated, took {1} ms...\r\n", maxDocs, DateTime.Now.Subtract(startDate).TotalMilliseconds);

				// delete the documents
				Console.WriteLine("Deleting {0} docs...", maxDocs);
				startDate = DateTime.Now;
				foreach (var rid in documentRids)
				{
					cn.DeleteDocument(rid);
				}
				Console.WriteLine("{0} deleted, took {1} ms...\r\n", maxDocs, DateTime.Now.Subtract(startDate).TotalMilliseconds);

			}
			Console.WriteLine("Done");
			Console.Read();
		}

		static void EnsureClasses(Connection cn)
		{
			// Create class user 
			// user
			//	- username STRING
			//  - age BYTE
			var cls = cn.Classes.FirstOrDefault(o => o.Name == "user");
			if (cls == null)
			{
				// create class using SQL
				cn.ExecuteSqlCommand("CREATE CLASS user");

			}
			if (cls == null || cls.Properties.Count(o => o.Name == "username") == 0)
			{
				cn.ExecuteSqlCommand("CREATE PROPERTY user.username STRING");
			}
			if (cls == null || cls.Properties.Count(o => o.Name == "age") == 0)
			{
				cn.ExecuteSqlCommand("CREATE PROPERTY user.age BYTE");
			}
			Console.WriteLine("Class user created.");

		}
	}
}
